package com.uu.custom.yingbang.facade.restful.service.impl;

import com.alibaba.fastjson.JSON;
import com.uu.custom.yingbang.facade.restful.RestfulUtil;
import com.uu.custom.yingbang.facade.restful.common.Code;
import com.uu.custom.yingbang.facade.restful.common.TrackingStatus;
import com.uu.custom.yingbang.facade.restful.model.SelectTrackResponse;
import com.uu.custom.yingbang.facade.restful.model.TrackInfo;
import com.uu.custom.yingbang.facade.restful.model.Tracking;
import com.uu.custom.yingbang.facade.restful.service.TrackingService;
import org.springframework.stereotype.Service;
import org.uu.skull.result.Result;
import org.uu.skull.result.ResultCreator;
import org.uu.skull.util.CollectionUtil;
import org.uu.skull.util.StringUtil;

import java.util.*;

/**
 * @Author LiuKe
 * @Date 2017/9/19 23:33
 */
@Service
public class TrackingServiceImpl implements TrackingService {
    @Override
    public Result getInfo(String trackingNumbers) {
        //测试环境
//        String gateWay = "http://www.sz56t.com:8082";
//        String method = "/selectTrack.htm";
        //正式环境
        String gateWay = "http://139.159.216.92:8082";
        String method = "/selectTrack.htm";
        Map<String,String> map = new HashMap<>();
        StringBuilder sb = new StringBuilder();
        String documentCode = "";
        Tracking tracking = JSON.parseObject(trackingNumbers,Tracking.class);
        if(StringUtil.isNotEmpty(tracking.getTrackingNumber())){
            String[] trackingNumberArray = tracking.getTrackingNumber().split(",");
            Set<String> set = new LinkedHashSet<>();
            for(String trackingNumber : trackingNumberArray){
                trackingNumber = trackingNumber.trim();
                if(StringUtil.isNotEmpty(trackingNumber)){
                    set.add(trackingNumber);
                }
            }
            for(String s : set){
                sb.append(s).append(",");
            }
            if(sb.length()>0){
                documentCode = sb.substring(0,sb.length()-1);
            }
            map.put("documentCode",documentCode);

            String result = RestfulUtil.post(gateWay+method,map);
            try{
                List<SelectTrackResponse> selectTrackResponseList = JSON.parseArray(result, SelectTrackResponse.class);
                List<TrackInfo> trackInfoList = selectTrackResponseList.get(0).getData();
                if(CollectionUtil.isEmpty(trackInfoList)){
                    trackInfoList = new ArrayList<>();
                    for(String trackingNumber : set){
                        TrackInfo trackInfo = new TrackInfo();
                        trackInfo.setTrackingNumber(trackingNumber);
                        trackInfo.setStatus(TrackingStatus.TRACKING_STATUS_NOT_FIND);
                        trackInfoList.add(trackInfo);
                    }
                }else{
                    Map<String,String> m = new HashMap<>();
                    for(TrackInfo trackInfo : trackInfoList){
                        if("签收".equals(trackInfo.getBusinessStatus())){
                            trackInfo.setStatus(TrackingStatus.TRACKING_STATUS_OK);
                        }else if("转运中".equals(trackInfo.getBusinessStatus())){
                            trackInfo.setStatus(TrackingStatus.TRACKING_STATUS_SENDING);
                        }else{
                            trackInfo.setStatus(TrackingStatus.TRACKING_STATUS_NOT_FIND);
                        }
                        m.put(trackInfo.getTrackingNumber(),"");
                    }
                    for(String s : set){
                        if(!m.containsKey(s)){
                            TrackInfo trackInfo = new TrackInfo();
                            trackInfo.setTrackingNumber(s);
                            trackInfo.setStatus(TrackingStatus.TRACKING_STATUS_NOT_FIND);
                            trackInfoList.add(trackInfo);
                        }
                    }
                }
                return ResultCreator.create(Code.SUCCESS,trackInfoList);
            }catch (Exception e){
                e.printStackTrace();
                return ResultCreator.create(Code.SELECT_TRACKING_ERROR);
            }
        }
        return ResultCreator.create(Code.SUCCESS);
    }
}
